package com.yztc.administrator.meishi.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.yztc.administrator.meishi.R;

import java.util.HashSet;
import java.util.Set;

public class VideoActivity extends AppCompatActivity {

    private VideoView vv;
    private TextView tvGood;
    private TextView tvCap;
    private String caption;
    private String url;
    private SharedPreferences.Editor edit;
    private SharedPreferences sharedPreferences;
    private String screen_name;
    private TextView tvUsername;
    private String likes_count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        //初始化控件
        init();
        //得到意图中的数据
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        caption = intent.getStringExtra("caption");
        likes_count = intent.getStringExtra("likes_count");
        screen_name = intent.getStringExtra("screen_name");
        //
        initVideo();

        sharedPreferences = getSharedPreferences("video", MODE_PRIVATE);
        edit = sharedPreferences.edit();
    }

    /* *
       * @author: ZTC
       * @Description: 初始化video控件功能
       * @data:  2016/9/23  9:48
       * @version: V1.0
       */
    private void initVideo() {
        Uri parse = Uri.parse(url);
        vv.setVideoURI(parse);
        vv.start();
        tvCap.setText(caption);
        tvGood.setText(likes_count);
        tvUsername.setText(screen_name);
    }

    //初始化邮件
    private void init() {
        tvUsername = (TextView) findViewById(R.id.tv_username);
        tvCap = (TextView) findViewById(R.id.tv_jump);
        tvGood = (TextView) findViewById(R.id.tv_jump_dianzan);
        vv = (VideoView) findViewById(R.id.vv);
    }

    //收藏点击事件
    public void collection(View view) {
        //创建set存储数据
        Set<String> set = new HashSet<>();
        set.add(caption);
        set.add(url);
        //将数据存储到本地
        edit.putStringSet(caption,set);
        edit.commit();
        //按照本地数据长度判断是否收藏成功
        if (sharedPreferences.getAll().size() != 0){
            Toast.makeText(VideoActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(VideoActivity.this, "收藏失败", Toast.LENGTH_SHORT).show();
        }
    }
}
